﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.Common.Public;

namespace RFIDStockManagement.DAL
{
    abstract class BasicAccess
    {
        protected string m_TableName = string.Empty;
        public BasicAccess(string tableName)
        {
            m_TableName = tableName;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="info"></param>
        public abstract BasicModelInfo Save(BasicModelInfo info);
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="lstInfo"></param>
        public abstract List<string> Delete(List<string> lstInfo);
        /// <summary>
        /// 获取字段列表
        /// </summary>
        /// <returns></returns>
        protected virtual string GetFieldList()
        {
            return "*";
        }
        /// <summary>
        /// 模糊资料列表
        /// </summary>
        /// <param name="dctFilter"></param>
        /// <returns></returns>
        public virtual DataTable Search(Dictionary<string, object> dctFilter, bool isAnd)
        {
            DbHelper helper = new DbHelper();

            string strFilter = string.Empty;
            string sql = string.Empty;
            if (dctFilter != null && dctFilter.Count > 0)
            {
                foreach (KeyValuePair<string, object> filter in dctFilter)
                {
                    strFilter += string.Format(" {0} LIKE '%{1}%' {2}", filter.Key, filter.Value, isAnd ? "AND":"OR ");
                }
                strFilter = strFilter.Substring(0, strFilter.Length - 3);
                sql = string.Format("SELECT TOP {0} {3} FROM {2} WHERE {1} ORDER BY FID DESC",
                    PublicConst.SelectTopCount, strFilter, m_TableName, GetFieldList());

                DbCommand comm = helper.GetSqlStringCommand(sql);
                return helper.ExecuteDataTable(comm);
            }

            return null;
        }
        /// <summary>
        /// 获取资料列表
        /// </summary>
        /// <param name="dctFilter"></param>
        /// <returns></returns>
        public virtual DataTable GetList(Dictionary<string, object> dctFilter, bool isAnd)
        {
            DbHelper helper = new DbHelper();

            string strFilter = string.Empty;
            string sql = string.Empty;
            List<SqlParameter> parameter = new List<SqlParameter>();
            if (dctFilter != null && dctFilter.Count > 0)
            {
                foreach (KeyValuePair<string, object> filter in dctFilter)
                {
                    strFilter += string.Format(" {0}=@{0} {1}", filter.Key, isAnd ? "AND" : "OR ");
                    parameter.Add(new SqlParameter("@" + filter.Key, filter.Value));
                }
                strFilter = strFilter.Substring(0, strFilter.Length - 3);
                sql = string.Format("SELECT TOP {0} {3} FROM {2} WHERE {1} ORDER BY FID DESC",
                    PublicConst.SelectTopCount, strFilter, m_TableName, GetFieldList());
            }
            else
            {
                sql = string.Format("SELECT TOP {0} {2} FROM {1} ORDER BY FID DESC",
                    PublicConst.SelectTopCount, m_TableName, GetFieldList());
            }

            DbCommand comm = helper.GetSqlStringCommand(sql);
            comm.Parameters.AddRange(parameter.ToArray());

            return helper.ExecuteDataTable(comm);
        }
        /// <summary>
        /// 读取一个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual BasicModelInfo GetOne(long id)
        {
            BasicModelInfo result = null;
            string sql = string.Format("SELECT * FROM {0} WHERE FID={1}", this.m_TableName, id);
            DbHelper helper = new DbHelper();
            DbCommand comm = helper.GetSqlStringCommand(sql);
            using (DbDataReader reader = helper.ExecuteReader(comm))
            {
                result = this.CreateModelInfo(reader);
                this.SetBeQuoteInfo(result);
            }

            return result;
        }
        protected abstract BasicModelInfo CreateModelInfo(DbDataReader reader);
        /// <summary>
        /// 设置被引用信息
        /// </summary>
        /// <param name="info"></param>
        protected abstract void SetBeQuoteInfo(BasicModelInfo info);
    }
}
